
const app = getApp(); 
Page({
  data: {
    isSearch: false,
    result: [],
    value: '',
    hotCity:  ['北京市', '深圳市', '广州市', '上海市', '香港', '哈尔滨'],
    history: []
  },
  inputHandle(e) {
    
    const { value } = e.detail
    console.log(value);
    if (value) {
      wx.request({
        url: 'http://localhost:8900/api/getweather/weatherInfo',
        data:{
          city:value
        },
        success:function(res){
          console.log('-------',res)
        },
        fail:function(res){},

      })
    }
  },
  onLoad() {
    this.setData({
      history: this.getLocal()
    })
    // console.log("--------",app.globalData.hotCity);
  },
  cancelHandle() {
    wx.navigateBack()
  },
  selectItem(e) {
    const { lat, lng } = e.currentTarget.dataset
    wx.navigateTo({
      url: `../weather/weather?lat=${lat}&lng=${lng}`
    })
  },
  clearHandle() {
    this.setData({
      value: ''
    })
  },
  clearHistory() {
    wx.removeStorage({ key: 'history'})
    this.setData({
      history: []
    })
  },
  dwHandle() {
    wx.navigateTo({
      url: `../weather/weather`
    })
  },
  hotHandle(e) {
    const { value } = e.currentTarget.dataset
    this.setLocal(value)
    // QQMap.geocoder({
    //   address: value,
    //   success: (res) => {
    //     const { location: { lat, lng } } = res.result
    //     wx.navigateTo({
    //       url: `../weather/weather?lat=${lat}&lng=${lng}`
    //     })
    //   }
    // });
  },
  // 设置历史记录
  setLocal(val) {
    const arr = this.getLocal()
    if (!arr.includes(val)) {
      console.log(arr)
      arr.push(val)
      wx.setStorageSync('history', arr)
    }
  },
  getLocal() {
    return wx.getStorageSync('history') ? wx.getStorageSync('history') : []
  },
  delHandle(e) {
    const { value } = e.currentTarget.dataset
    const arr = this.getLocal()
    if (arr.includes(value)) {
      const index = arr.indexOf(value)
      arr.splice(index, 1)
      wx.setStorageSync('history', arr)
      this.setData({
        history: arr
      })
    }
  }
})